#include <iostream>
using namespace std;

class wrapper;

class ref {
	public:

	ref(const ref& r):i(r.i){}
	const int& get() const {
		cout << i << endl;
		return i;
	}

	ref(int j): i(int(j)) {}
	
	private:
		const int & i;
		friend int main();
		friend class wrapper;
		friend wrapper create_wrapper();
};

class wrapper {

	public:
			wrapper(const ref& ri) : __ri(ri) {}
			wrapper(const wrapper& wr) : __ri(wr.__ri) {}
			void get() {
				__ri.get();
			}
	private:
			const ref& __ri;
};

wrapper create_wrapper() {
	int *i = new int(10);
	wrapper wr(*i);
	delete i;
	return wr;
}

int main(){

	int *i=new int(10);

	ref r(*i);

	delete i;

	r.get();

	wrapper w(create_wrapper());
	w.get();
	return 0;
}
